/**
 * @(#)MD5Util.java, 2013-6-15. 
 * 
 * Copyright 2013 Yodao, Inc. All rights reserved.
 * YODAO PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.credit.util;

import java.math.BigInteger;
import java.security.MessageDigest;

/**
 * @author huahui
 */
	//信息摘要算法类
public class MD5Util {
    private static MessageDigest md5 = null;	//MessageDigest类是信息摘要算法功能的类，它可以接收任意大小的数据，并输出固定长度的哈希值（作用就是用来加密的）
    static {
        try{
            md5 = MessageDigest.getInstance("MD5");//getInstance()方法是返回指定摘要算法的MessageDigest对象
        } catch(Exception e) {
            System.out.println(e.getMessage());
        }
    }
    /***
     * 获取一个String的md5值
     * @param str
     * @return
     */
    public final static String MD5(String str) {
        byte[] bs = md5.digest(str.getBytes());	//将传入的参数转化成bytes类型的数组
        StringBuilder sb = new StringBuilder(40);
        for(byte x:bs) {
            if((x & 0xff)>>4 == 0) {
                sb.append("0").append(Integer.toHexString(x & 0Xff));
            } else {
                sb.append(Integer.toHexString(x & 0xff));
            }
        }
        System.out.println(sb+"-----");
        return sb.toString();
    }
    
    /**
     * 获取一个String的md5数值
     * @param str
     * @return
     */
    public static BigInteger getMd5Num(String str) {
        byte[] bs = md5.digest(str.getBytes());
        BigInteger num = BigInteger.ZERO;
        for(byte x:bs) {
            num = num.shiftLeft(4);
            num = num.add(BigInteger.valueOf(x & 0xff));
        }
        return num;
        
    }
    public static void main(String[] args) {
        System.out.println(MD5Util.MD5("884101"));
       // System.out.println(MD5Util.getMd5Num("a"));
    }
}
 